package com.example.service;

import com.example.DTO.OrderselectDTO;
import com.example.entity.Order;
import com.example.entity.StandardItem;
import com.example.mapper.OrderItemDao;
import com.example.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class OrderService {
    @Autowired
    private OrderMapper ordermapper;

    @Autowired
    private OrderItemDao orderItemDao;


    public List<OrderselectDTO> selectbyid(Integer buyerid){
        return ordermapper.selectbyid(buyerid);
    }


    //这里就是查询所有订单啦并没有更改哦,已经不用啦
   /* public List<OrderselectDTO> selectorders() {
        return ordermapper.selectorders();
    }*/

    public void addOrders(Order order) {
            ordermapper.createorders(order);
    }
    public void confirmation(Integer id) {
        orderItemDao.confirmation(id);
        orderItemDao.updatedisplayStatus(id,"商家已确认,现为备货状态");
    }
    public void stockup(Integer id) {
        orderItemDao.stockUp(id);
        orderItemDao.updatedisplayStatus(id,"商家已备货，现为待发货状态");
    }
    //更改相关内容。。。
    public void sendorder(Integer id) {
        orderItemDao.sendorder(id);
        orderItemDao.updatedisplayStatus(id,"商家已发货，现为待收货状态");
    }
    //更改相关内容
    public void completion(Integer id) {
        orderItemDao.completion(id);
        orderItemDao.updatedisplayStatus(id,"买家已收货，交易完成");
    }

    public void cancelorder(Integer id) {
        orderItemDao.cancelorder(id);
    }

    public boolean isconfirmation(Integer id) {
        if (id == null) return false;
        StandardItem item = orderItemDao.queryByitemId(id);
        return item != null && Objects.equals(item.getConfirmation(), 1);
    }
    //
    public boolean issendorder(Integer id) {
        if (id == null) return false;
        StandardItem item = orderItemDao.queryByitemId(id);
        return item != null && Objects.equals(item.getSendOrder(), 1);
    }
    //更改商家确认是所需用到的表
    public boolean iscompletion(Integer id) {
        if (id == null) return false;
        StandardItem item = orderItemDao.queryByitemId(id);
        return item != null && Objects.equals(item.getCompletion(), 1);
    }
    public Integer isstock(Integer productid) {
       return ordermapper.isstock(productid);
    }

    public void reducestock(Integer productid) {
        ordermapper.reducestock(productid);
    }
    //已经更改为Order_items表
    public boolean isstockup(Integer id) {
        if (id == null) return false;
        StandardItem item = orderItemDao.queryByitemId(id);
        return item != null && Objects.equals(item.getStockUp(), 1);
    }
}
